import Vue from 'vue'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Router from 'vue-router'
import Layout from 'views/layout/Layout'
import Login from 'views/auth/Login'
import Dashboard from 'views/dashboard/Dashboard'
import Binding from 'views/auth/Binding'
import E404 from 'views/error/404'
import E401 from 'views/error/401'
import Post from 'views/post/Items'
import CreatePost from 'views/post/Create'
import PostCategory from 'views/post/Categories'
import Permission from 'views/permission/Items'
import Role from 'views/role/Items'
import SetPermissions from 'views/role/SetPermissions'
import User from 'views/user/Items'
import Ad from 'views/ad/Items'
import Position from 'views/ad/Position'
import GoodsCategory from 'views/goods/Categories'
import GoodsType from 'views/goods/Types'
import GoodsAttr from 'views/goods/Attrs'
import GoodsSpec from 'views/goods/Spec'
import GoodsItem from 'views/goods/Items'
import GoodsComment from 'views/goods/Comments'
import CreateGoods from 'views/goods/Create'
// import TeamActivity from 'views/promotion/TeamActivity'
// import FlashSale from 'views/promotion/FlashSale'
// import Coupon from 'views/promotion/Coupon'
import Order from 'views/order/Items'
import OrderDetail from 'views/order/Show'
import Config from 'views/config/Items'
import Course from 'views/course/Course'
import CreateCourse from 'views/course/Create'
import Catalog from 'views/course/Catalog'
import CreateCourseContent from 'views/course/CreateCourseContent'
import Bonus from '@/views/bonus/Items'
import Subject from 'views/subjectSort/Items'
import Titlepurchase from 'views/subjectSort/Titlepurchase'
import Subjectcreate from 'views/subjectSort/Subjectcreate'
import Subjectcategories from 'views/subjectSort/Subjectcategories'
import Assessments from 'views/subjectSort/Assessments'
import Analysitical from 'views/subjectSort/Analysitical'
import Information from 'views/post/Information'
import Changes from 'views/changes/Items'
import CreateChanges from 'views/changes/Create'
import Distribution from 'views/changes/Distribution'
import ChangesCategory from 'views/changes/Categories'
import Addupdata from 'views/changes/Addupdata'
import Invite from 'views/invite/Items'
import SubjectcreateInvite from 'views/invite/Subjectcreate'
// import Invitecategories from 'views/invite/Subjectcategories'
import Forum from 'views/forum/Items'
// import SubjectcreateForum from 'views/forum/Subjectcreate'
import Forumcategories from 'views/forum/Subjectcategories'
Vue.use(Router)

export const constantRouterMap = [
  {
    path: '/login',
    component: Login,
    name: 'login',
    hidden: true
  },
  {
    path: '/binding',
    component: Binding,
    name: 'binding',
    hidden: true
  },
  {
    path: '/error',
    component: Layout,
    hidden: true,
    name: '错误',
    children: [
      { path: '/error/404', name: '404', component: E404 },
      { path: '/error/401', name: '401', component: E401 }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    icon: 'iconfont icon-dashboard',
    name: '控制台',
    children: [
      { path: '/dashboard', name: '首页', component: Dashboard, meta: {cache: false, title: 'dashboard'} }
    ]
  }
]

export const asyncRouterMap = [
  {
    path: '/goods',
    component: Layout,
    name: '产品管理',
    icon: 'iconfont icon-favorite',
    redirect: '/goods/index',
    children: [
      {path: '/goods/index', component: GoodsItem, name: '产品列表', meta: {permission: 'manage-product', cache: false}},
      {path: '/goods/create', component: CreateGoods, name: '添加产品', meta: {permission: 'manage-product', cache: false}},
      {path: '/goods/types', component: GoodsType, name: '产品模型', meta: {permission: 'manage-product', cache: false}},
      {path: '/goods/attrs', component: GoodsAttr, name: '产品属性', meta: {permission: 'manage-product', cache: false}},
      {path: '/goods/specs', component: GoodsSpec, name: '产品规格', meta: {permission: 'manage-product', cache: false}},
      {path: '/goods/edit/:id', component: CreateGoods, name: '编辑产品', hidden: true, meta: {permission: 'manage-product', cache: false, isEdit: true}},
      {path: '/goods/categories', component: GoodsCategory, name: '产品分类', meta: {permission: 'manage-category', cache: false}},
      {path: '/goods/comments', component: GoodsComment, name: '用户评论', meta: {permission: 'manage-product', cache: true}}
    ]
  },
  // {
  //   path: '/promotion',
  //   component: Layout,
  //   name: '促销管理',
  //   icon: 'iconfont icon-cart',
  //   children: [
  //     {path: '/promotion/team', component: TeamActivity, name: '拼团管理', meta: {permission: 'manage-promotion', cache: false}},
  //     {path: '/promotion/flashSale', component: FlashSale, name: '限时抢购', meta: {permission: 'manage-promotion', cache: false}},
  //     {path: '/promotion/coupon', component: Coupon, name: '优惠券', meta: {permission: 'manage-promotion', cache: false}}
  //   ]
  // },
  {
    path: '/order',
    component: Layout,
    name: '订单管理',
    icon: 'iconfont icon-manageorder',
    children: [
      {path: '/orders/index', component: Order, name: '订单列表', meta: {permission: 'manage-order', cache: false}},
      {path: '/orders/:id', component: OrderDetail, name: '订单详情', hidden: true, meta: {permission: 'manage-order', cache: false}}
    ]
  },
  {
    path: '/post',
    component: Layout,
    name: '文章管理',
    icon: 'iconfont icon-imagetext',
    children: [
      {path: '/post/index', component: Post, name: '文章列表', meta: {permission: 'manage-post', cache: false}},
      {path: '/post/inform', component: Information, name: '资讯列表', meta: {permission: 'manage-post', cache: false}},
      {path: '/post/create', component: CreatePost, name: '添加文章', meta: {permission: 'manage-post', cache: false}},
      {path: '/post/categories', component: PostCategory, name: '文章分类', meta: {permission: 'manage-post', cache: false}},
      {path: '/post/edit/:id', hidden: true, component: CreatePost, name: '编辑文章', meta: {permission: 'manage-post', isEdit: true, cache: false}}
    ]
  },
  {
    path: '/changes',
    component: Layout,
    name: '易经',
    icon: 'iconfont icon-imagetext',
    children: [
      {path: '/changes/index', component: Changes, name: '易经列表', meta: {permission: 'manage-changes', cache: false}},
      {path: '/changes/create', component: CreateChanges, name: '分析导语', meta: {permission: 'manage-changes', cache: false}},
      // {path: '/Addupdata/create', component: Addupdata, name: '分析', meta: {permission: 'manage-changes', cache: false}},
      {path: '/Distribution/:id', component: Distribution, name: '分析导语', hidden: true, meta: {permission: 'manage-changes', cache: false}},
      {path: '/changes/categories', component: ChangesCategory, name: '分析导语分类', meta: {permission: 'manage-changes', cache: false}},
      {path: '/Addupdata/edit/:id', hidden: true, component: Addupdata, name: '编辑易经', meta: {permission: 'manage-changes', isEdit: true, cache: false}}
    ]
  },
  {
    path: '/rbac',
    component: Layout,
    name: '用户权限',
    icon: 'iconfont icon-accountfilling',
    children: [
      {path: '/rbac/permission', component: Permission, name: '权限管理', meta: {permission: 'manage-permission'}},
      {path: '/rbac/role', component: Role, name: '角色管理', meta: {permission: 'manage-role'}},
      {path: '/rbac/role/permissions', component: SetPermissions, name: '权限设置', meta: {permission: 'manage-role', cache: true}},
      {path: '/rbac/user', component: User, name: '用户管理', meta: {permission: 'manage-user'}}
    ]
  },
  {
    path: '/ad',
    component: Layout,
    name: '广告管理',
    icon: 'iconfont icon-training',
    children: [
      {path: '/ad/position', component: Position, name: '广告位置', meta: {permission: 'manage-ad'}},
      {path: '/ad/index', component: Ad, name: '广告列表', meta: {permission: 'manage-ad'}}
    ]
  },
  {
    path: '/courses',
    component: Layout,
    name: '课程管理',
    icon: 'iconfont icon-training',
    children: [
      {path: '/courses/index', component: Course, name: '课程列表', meta: {permission: 'manage-course'}},
      {path: '/courses/catalog/:course', component: Catalog, name: '课程内容', hidden: true, meta: {permission: 'manage-course'}},
      {path: '/courses/create', component: CreateCourse, name: '创建课程', meta: {permission: 'manage-course'}},
      {path: '/courses/:id/edit', component: CreateCourse, name: '编辑课程', hidden: true, meta: {permission: 'manage-course', isEdit: true}},
      {path: '/courses/:id/content/create', component: CreateCourseContent, name: '创建课程内容', hidden: true, meta: {permission: 'manage-course'}},
      {path: '/courses/content/:id/edit', component: CreateCourseContent, name: '编辑课程内容', hidden: true, meta: {permission: 'manage-course', isEdit: true}}
    ]
  },
  {
    path: '/bonus',
    component: Layout,
    name: '资金管理',
    icon: 'iconfont icon-training',
    children: [
      {path: '/bonus', component: Bonus, name: '账户明细', meta: {permission: 'manage-bonus'}}
    ]
  },
  {
    path: '/configs',
    component: Layout,
    name: '系统设置',
    icon: 'iconfont icon-set',
    children: [
      {path: '/configs/index', component: Config, name: '系统', meta: {permission: 'manage-config'}}
    ]
  },
  {
    path: '/subject',
    component: Layout,
    name: '题库管理',
    icon: 'iconfont icon-imagetext',
    children: [
    {path: '/subject', component: Subject, name: '题目列表', meta: {permission: 'manage-subject', cache: false}},
    {path: '/Titlepurchase', component: Titlepurchase, name: '题目购买记录', meta: {permission: 'manage-subject', cache: false}},
    {path: '/Assessments', component: Assessments, name: '评估结果', meta: {permission: 'manage-subject', cache: false}},
    {path: '/Analysitical', component: Analysitical, name: '评估分析语句', meta: {permission: 'manage-subject', cache: false}},
    {path: '/subject/tcreate', component: Subjectcreate, name: '题目描述', meta: {permission: 'manage-subject', cache: false}},
    {path: '/subject/categories', component: Subjectcategories, name: '题目分类', meta: {permission: 'manage-subject', cache: false}}
    ]
  },
  {
    path: '/Invite',
    component: Layout,
    name: '人才招聘',
    icon: 'iconfont icon-imagetext',
    children: [
    {path: '/invite', component: Invite, name: '职位列表', meta: {permission: 'manage-invite', cache: false}},
    {path: '/Invite/tcreate', component: SubjectcreateInvite, name: '公司列表', meta: {permission: 'manage-invite', cache: false}}
    // {path: '/Invite/categories', component: Invitecategories, name: '题目分类', meta: {permission: 'manage-invite', cache: false}}
    ]
  },
  {
    path: '/forum',
    component: Layout,
    name: '论坛',
    icon: 'iconfont icon-imagetext',
    children: [
    {path: '/forum', component: Forum, name: '论坛列表', meta: {permission: 'manage-forum', cache: false}},
    {path: '/forum/categories', component: Forumcategories, name: '论坛分类', meta: {permission: 'manage-forum', cache: false}}
    ]
  },
  {
    path: '*',
    redirect: '/error/404',
    hidden: true
  }
]

const router = new Router({
  // 后端支持可开
  // mode: 'history',
  routes: constantRouterMap
})

const whiteList = ['/login', '/binding'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
  // 已经登录
  if (store.getters.token) {
    if (to.path === '/login') {
      next({path: '/'})
    } else {
      if (!store.getters.userInfo) {
        store.dispatch('getUserInfo').then((response) => {
          store.dispatch('generateRoutes', response.data.all_permissions).then(() => {
            router.addRoutes(store.getters.accessedRouters)
            next({ ...to })
          })
        }).catch((error) => {
          console.log(error)
        })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next({path: '/login'})
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
